/*
 * Copyright 2010
 * Jairou Software Organization
 * All Rights Reserved.
 * 
 * This software is the proprietary 
 * information of Roger Ramia
 * and the Jairou Software Organization 
 * Use is subject to license terms.
 * 
 * For licensing information please visit
 * http://www.jairou.org/license
 * 
 * @author Roger Ramia
 */
package org.jairou.web;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import org.jairou.core.Strings;

/**
 * Standard Jariou Filter implementation
 * providing support for xml defined request
 * handing.
 * <br/>
 * This class executes pre-configured
 * requests defined in xml (usually named request.xml)
 * 
 * @author Roger Ramia
 *
 */
public class JairouFilter 
implements Filter {
	
	private FilterConfig config;

	
	 /**
	  * The doFilter implementation inspects the
	  * current ServletRequest id (using Web.getId())
	  * and looks for a corresponding request node.  
	  * If no request xml is defined for the current ServletRequest
	  * the filter simply ignores the request.  
	  * <br/>
	  * If the request is found it looks for a specified handler
	  * as defined by the {@code<request handler="xxx">} property.  
	  * If no handler is found, simply delegates to the Web.handle()
	  * method. 
	  * <br/>
	  * The standard Web.handle() implementation simply performs
	  * the tasks defined within the <request> node for the given Id
	  * and forwards to the result of the handle() method.
	  * <br/>
	  * See org.jairou.Web.handle() for more information about
	  * the standard implementation.  Refer to the Jairou Users Guide
	  * at http://www.jairou.com for information about 
	  * implementing a custom Handler.
	  * 
	  */
	public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) 
	throws IOException, ServletException {
		try {
			HttpServletRequest request = (HttpServletRequest)req;
			if (Web.isDefined(request)) {
				String id = Web.getId(request);
				Object ctx = Web.getContext(request);
				String forward = null;
				try {				
					//String handler =  XML.get("request."+id+".handler");
					//if (handler != null) forward = Functions.call(handler, ctx);
					//else forward = Web.handle(id, ctx);
					forward = Web.handle(id, ctx);
				}
				finally {
					if (Strings.nullOrEmpty(forward)) chain.doFilter(req, resp);
					else request.getRequestDispatcher(forward).forward(req, resp);
				}
			}
			else chain.doFilter(req, resp);
		}
		catch(Exception e) {
			throw new ServletException(e);
		}
	}

	/**
	 * simply caches the FilterConfig
	 */
	public void init(FilterConfig config) 
	throws ServletException {
		this.config = config;
	}
	
	/**
	 * sets the FilterConfig to null
	 */
	public void destroy() {
		this.config = null;
	}
}